package com.cu.employmentmanagementsystem.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.cu.employmentmanagementsystem.common.exception.CommonException;
import com.cu.employmentmanagementsystem.common.result.R;
import com.cu.employmentmanagementsystem.dao.StudentRecruitmentDao;
import com.cu.employmentmanagementsystem.entity.RecruitInformation;
import com.cu.employmentmanagementsystem.dao.RecruitInformationDao;
import com.cu.employmentmanagementsystem.entity.User;
import com.cu.employmentmanagementsystem.service.RecruitInformationService;
import com.cu.employmentmanagementsystem.service.UserService;
import com.cu.employmentmanagementsystem.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RecruitInformation)表服务实现类
 *
 * @author makejava
 * @since 2024-07-02 16:20:17
 */
@Service("recruitInformationService")
public class RecruitInformationServiceImpl implements RecruitInformationService {
    @Resource
    private RecruitInformationDao recruitInformationDao;

    @Autowired
    private UserService userService;

    @Autowired
    private StudentRecruitmentDao studentRecruitmentDao;

    @Override
    public R commitRecruit(RecruitInformation recruitInformation) {
        try {
            //获取userid赋值给companyid
            Long companyId = CommonUtils.getCompanyId();
            recruitInformation.setCompanyId(companyId);
            //赋值审核状态
            recruitInformation.setStatus(-1);
            //添加招聘信息至数据库,判断是否添加成功
            int y = recruitInformationDao.insert(recruitInformation);
            //发布招聘信息成功
            if(y == 1){
                return R.success(y);
            }
            //发布招聘信息失败
            else{
                return R.error("发布招聘信息失败！");
            }
        }catch (Exception e) {
            throw new CommonException("发布招聘信息失败！");
        }
    }
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RecruitInformation queryById(Long id) {
        return this.recruitInformationDao.queryById(id);
    }

    /**
     * 根据审核状态查询招聘信息
     *
     * @param status 审核状态
     * @return 影响行数
     */
    @Override
    public List<RecruitInformation> queryByStatus(String status) {

        return recruitInformationDao.queryByStatus(status);
    }

    /**
     * 分页查询
     *
     * @param recruitInformation 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<RecruitInformation> queryByPage(RecruitInformation recruitInformation, PageRequest pageRequest) {
        long total = this.recruitInformationDao.count(recruitInformation);
        return new PageImpl<>(this.recruitInformationDao.queryAllByLimit(recruitInformation, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param recruitInformation 实例对象
     * @return 实例对象
     */
    @Override
    public RecruitInformation insert(RecruitInformation recruitInformation) {
        this.recruitInformationDao.insert(recruitInformation);
        return recruitInformation;
    }

    /**
     * 修改招聘信息
     *
     * @param recruitInformation 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(RecruitInformation recruitInformation) {
        //判断是否被学生申请
        Long recruitmen_id = recruitInformation.getId();
        if (this.studentRecruitmentDao.queryByRecruitId(recruitmen_id) > 0){
            throw new CommonException("该招聘信息已被学生申请，不能修改！");
        }
        recruitInformation.setStatus(-1);
        return this.recruitInformationDao.update(recruitInformation) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        //判断是否被学生申请
        if (this.studentRecruitmentDao.queryByRecruitId(id) > 0){
            throw new CommonException("该招聘信息已被学生申请，不能删除！");
        }
        return this.recruitInformationDao.deleteById(id) > 0;
    }
}
